package com.xmtlxx.tms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.utils.StringUtils;
import com.xmtlxx.tms.domain.CarType;
import com.xmtlxx.tms.domain.bo.CarTypeBo;
import com.xmtlxx.tms.domain.vo.CarTypeVo;
import com.xmtlxx.tms.mapper.CarTypeMapper;
import com.xmtlxx.tms.mapper.CarrierMapper;
import com.xmtlxx.tms.service.ICarTypeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmtlxx.system.domain.SysOss;
import com.xmtlxx.system.mapper.SysOssMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 车型Service业务层处理
 *
 * @author xmtlxx
 * @date 2022-08-15
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class CarTypeServiceImpl implements ICarTypeService {

    private final CarTypeMapper baseMapper;

    private final SysOssMapper ossMapper;

    private final CarrierMapper carrierMapper;

    /**
     * 查询车型
     *
     * @param id 车型主键
     * @return 车型
     */
    @Override
    public CarTypeVo queryById(String id){
        //return baseMapper.selectVoById(id);
        return baseMapper.selectCarTypeVoByID(id);
    }

    /**
     * 查询车型列表
     *
     * @param bo 车型
     * @return 车型
     */
    @Override
    public TableDataInfo<CarTypeVo> queryPageList(CarTypeBo bo, PageQuery pageQuery) {
       // LambdaQueryWrapper<CarType> lqw = buildQueryWrapper(bo);
       // Page<CarTypeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<CarTypeVo> result = baseMapper.selectPageCarTypeList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询车型列表
     *
     * @param bo 车型
     * @return 车型
     */
    @Override
    public List<CarTypeVo> queryList(CarTypeBo bo) {
       // LambdaQueryWrapper<CarType> lqw = buildQueryWrapper(bo);
       // return baseMapper.selectVoList(lqw);
        return baseMapper.selectCarTypeList(bo);
    }

    private LambdaQueryWrapper<CarType> buildQueryWrapper(CarTypeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CarType> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCarrierId()), CarType::getCarrierId, bo.getCarrierId());
        lqw.eq(bo.getDeptId() != null, CarType::getDeptId, bo.getDeptId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), CarType::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getCarTypeNo()), CarType::getCarTypeNo, bo.getCarTypeNo());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), CarType::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), CarType::getCreateBy, bo.getCreateBy());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            CarType::getCreateTime ,params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.eq(StringUtils.isNotBlank(bo.getDelFlag()), CarType::getDelFlag, bo.getDelFlag());
        return lqw;
    }

    /**
     * 新增车型
     *
     * @param bo 车型
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(CarTypeBo bo) {
        CarType add = BeanUtil.toBean(bo, CarType.class);
        validEntityBeforeSave(add);

        //填充UUID
        add.setId(IdUtil.simpleUUID());
        add.setDelFlag("0");
        add.setDeptId(carrierMapper.selectById(add.getCarrierId()).getDeptId());

        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }

        if(bo.getOss() != null && bo.getOss().size() > 0){
            for (SysOss oss:bo.getOss()){
                SysOss sysOss = new SysOss();
                BeanUtil.copyProperties(oss,sysOss);
                sysOss.setObjid(add.getId());
                sysOss.setBuss("car_type");
                ossMapper.insertOrUpdate(sysOss);
            }
        }

        return flag;
    }

    /**
     * 修改车型
     *
     * @param bo 车型
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(CarTypeBo bo) {
        CarType update = BeanUtil.toBean(bo, CarType.class);
        validEntityBeforeSave(update);

        update.setDeptId(carrierMapper.selectById(update.getCarrierId()).getDeptId());

        if(bo.getOss() != null && bo.getOss().size() > 0){
            for (SysOss oss:bo.getOss()){
                SysOss sysOss = new SysOss();
                BeanUtil.copyProperties(oss,sysOss);
                sysOss.setObjid(update .getId());
                sysOss.setBuss("car_type");
                ossMapper.insertOrUpdate(sysOss);
            }
        }

        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(CarType entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除车型
     *
     * @param ids 需要删除的车型主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
